package com.tdd;

import java.math.BigDecimal;

public class Factorial implements Operation {
	public void operate(HpStack values) {
		BigDecimal operand = values.pop();

		switch (BigDecimal.ZERO.compareTo(operand)) {
		case 1:
			throw new RuntimeException("No Factorial for Negative Numbes");
		case 0:
			values.push(BigDecimal.ONE);
			return;
		case -1:
			BigDecimal result = BigDecimal.ONE;
			while (BigDecimal.ONE.compareTo(operand) < 0) {
				result = result.multiply(operand);
				operand = operand.subtract(BigDecimal.ONE);
			}

			values.push(result);
		};
	}
}
